package com.probox.modules.controller.product;

import com.probox.common.core.enums.SpecsType;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.common.entity.domain.product.ProductSpecs;
import com.probox.common.entity.service.product.IProductSpecsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 商品规格/属性Controller
 *
 *
 * @date 2021-05-27
 */
@RestController
@RequestMapping("/product/specs")
public class ProductSpecsController extends BaseController {
    @Autowired
    private IProductSpecsService productSpecsService;

    /**
     * 查询商品规格/属性列表
     */
    @PreAuthorize(hasPermi = "product:specs:list")
    @GetMapping("/list")
    public TableDataInfo list(ProductSpecs productSpecs) {
        productSpecs.setSpecsType(SpecsType.specs.getKey());
        startPage();
        List<ProductSpecs> list = productSpecsService.queryList(productSpecs);
        return getDataTable(list);
    }

    /**
     * 导出商品规格/属性列表
     */
    @PreAuthorize(hasPermi = "product:specs:export")
    @Log(title = "商品规格/属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductSpecs productSpecs) throws IOException {
        List<ProductSpecs> list = productSpecsService.queryList(productSpecs);
        ExcelUtil<ProductSpecs> util = new ExcelUtil<ProductSpecs>(ProductSpecs.class);
        util.exportExcel(response, list, "商品规格/属性数据");
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/bulidSelect")
    public AjaxResult bulidSelect() {
        return AjaxResult.success(productSpecsService.bulidSelect());
    }
    /**
     * 获取商品规格/属性详细信息
     */
    @PreAuthorize(hasPermi = "product:specs:query")
    @GetMapping(value = "/{specsUid}")
    public AjaxResult getInfo(@PathVariable("specsUid") Long specsUid) {
        return AjaxResult.success(productSpecsService.selectProductSpecsById(specsUid));
    }

    /**
     * 新增商品规格/属性
     */
    @PreAuthorize(hasPermi = "product:specs:add")
    @Log(title = "商品规格/属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductSpecs productSpecs) {
        productSpecs.setSpecsType(SpecsType.specs.getKey());
        return toAjax(productSpecsService.insertProductSpecs(productSpecs));
    }

    /**
     * 修改商品规格/属性
     */
    @PreAuthorize(hasPermi = "product:specs:edit")
    @Log(title = "商品规格/属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductSpecs productSpecs) {
        productSpecs.setSpecsType(SpecsType.specs.getKey());
        return toAjax(productSpecsService.updateProductSpecs(productSpecs));
    }

    /**
     * 删除商品规格/属性
     */
    @PreAuthorize(hasPermi = "product:specs:remove")
    @Log(title = "商品规格/属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{specsUids}")
    public AjaxResult remove(@PathVariable Long[] specsUids) {
        return toAjax(productSpecsService.deleteProductSpecsByIds(specsUids));
    }
}
